<template>
  <el-main>
    <div class="margin-bottom-20">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/device' }"
          >设备管理
        </el-breadcrumb-item>
        <el-breadcrumb-item>库存列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="clearfix">
      <el-form class="pull-left" inline>
        <el-form-item>
          <el-input
            placeholder="请输入imei"
            v-model="search_param.imei"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-select placeholder="通信方式" v-model="search_param.deviceSocket">
            <el-option
              :key="item.code"
              :label="item.label"
              :value="item.code"
              v-for="item in device_socket"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select placeholder="设备类型" v-model="search_param.deviceType">
            <el-option
              :key="item.code"
              :label="item.label"
              :value="item.code"
              v-for="item in device_type"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select placeholder="平台信息" v-model="search_param.platformType">
            <el-option
              :key="item.code"
              :label="item.label"
              :value="item.code"
              v-for="item in platform_type"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button-group>
            <el-button
              @click="handleSearch"
              icon="el-icon-search"
              size="medium"
              type="primary"
              >搜索
            </el-button>
            <el-button @click="handleReset" size="medium" type="primary"
              >重置
            </el-button>
          </el-button-group>
        </el-form-item>
      </el-form>
    </div>
    <el-card class="box-card" shadow="never">
      <el-table :data="table_data" stripe style="width: 100%">
        <el-table-column label="设备状态" width="80">
          <template slot-scope="scope">
            <el-tag disable-transitions type="success" v-if="!scope.row.isLock">
              在线
            </el-tag>
            <el-tag disable-transitions type="danger" v-else>
              离线
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="位置名称"
          prop="deviceType_desc"
          show-overflow-tooltip
        />
        <el-table-column
          label="设备类型"
          prop="deviceType_desc"
          show-overflow-tooltip
        />
        <el-table-column label="imei" prop="imei" show-overflow-tooltip>
        </el-table-column>
        <el-table-column
          label="通信方式"
          prop="deviceSocket_desc"
          show-overflow-tooltip
        />
        <el-table-column
          label="平台信息"
          prop="platformType_desc"
          show-overflow-tooltip
        />
        <el-table-column
          label="设备波段(MHz)"
          prop="waveLength"
          show-overflow-tooltip
          width="120"
        />

        <el-table-column
          label="剩余时长(天)"
          prop="initDate"
          show-overflow-tooltip
          width="120"
        />
        <el-table-column
          label="所属公司"
          prop="company_name"
          show-overflow-tooltip
        />
        <el-table-column label="操作" width="240">
          <template slot-scope="scope">
            <el-button
              @click="handleDelete(scope.row)"
              size="mini"
              type="text"
              style="color: red"
              >暂停
            </el-button>
            <el-button @click="handleDetail(scope.row)" size="mini" type="text"
              >设备详情
            </el-button>
            <el-button @click="handleReport(scope.row)" size="mini" type="text"
              >上报历史
            </el-button>
            <el-button
              @click="handleInstruct(scope.row)"
              size="mini"
              type="text"
              >下发指令
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="margin-top-10 clearfix">
        <el-pagination
          :current-page.sync="current_page"
          :page-size.sync="page_size"
          :page-sizes="[5, 10, 20]"
          :total="total_size"
          @current-change="load_table_data"
          @size-change="load_table_data"
          background
          class="pull-right"
          layout="total, sizes,  prev, pager, next"
        />
      </div>
    </el-card>
  </el-main>
</template>

<script>
import { mapActions, mapState } from "vuex";

export default {
  data() {
    return {
      device_type: [],
      device_socket: [],
      platform_type: []
    };
  },
  computed: {
    ...mapState("device_bind", ["total_size", "table_data"]),
    page_size: {
      get() {
        return this.$store.state.device_bind.page_size;
      },
      set(value) {
        this.set_page_size(value);
      }
    },
    current_page: {
      get() {
        return this.$store.state.device_bind.current_page;
      },
      set(value) {
        this.set_current_page(value);
      }
    },
    search_param: {
      get() {
        return this.$store.state.device_bind.search_param;
      }
    }
  },
  mounted() {
    this.load_table_data();
    this.load_device_type();
    this.load_device_socket();
    this.load_platform_type();
  },
  methods: {
    ...mapActions("device_bind", [
      "load_table_data",
      "set_page_size",
      "set_current_page",
      "set_search_param"
    ]),
    handleSearch() {
      this.set_search_param(Object.assign({}, this.search_param));
      this.load_table_data();
    },
    handleReset() {
      this.set_search_param({});
      this.load_table_data();
    },
    handleDetail(row) {
      this.$router.push("/device/bind/detail/" + row.id);
    },
    handleInstruct(row) {
      this.$router.push("/device/bind/instruct/" + row.id);
    },
    handleReport(row) {
      this.$router.push("/device/bind/report/" + row.id);
    },
    load_device_type() {
      this.$ajax.support
        .device_type()
        .then(result => {
          if (result.code === 1) {
            this.device_type = result.data;
          }
        })
        .catch(e => {
          console.log(e);
        });
    },
    load_device_socket() {
      this.$ajax.support
        .device_socket()
        .then(result => {
          if (result.code === 1) {
            this.device_socket = result.data;
          }
        })
        .catch(e => {
          console.log(e);
        });
    },
    load_platform_type() {
      this.$ajax.support
        .platform_type()
        .then(result => {
          if (result.code === 1) {
            this.platform_type = result.data;
          }
        })
        .catch(e => {
          console.log(e);
        });
    }
  }
};
</script>
